<html>
<head><meta charset="utf-8"><title>GlobalAlloc::realloc guarantees · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html">GlobalAlloc::realloc guarantees</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="227914135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227914135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vytautas Astrauskas [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227914135">(Feb 26 2021 at 11:26)</a>:</h4>
<p>Hello, I am a bit confused about the documentation of <a href="https://doc.rust-lang.org/alloc/alloc/trait.GlobalAlloc.html#method.realloc">GlobalAlloc::realloc</a>: what guarantees this function give about the reallocated block of memory when the reallocation succeeds? Could, for example, sound implementation of <code>GlobalAlloc</code> return a memory block that contains only garbage values? I do not see anything in the documentation that would describe what can be assumed about the returned memory block.</p>



<a name="227939803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227939803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227939803">(Feb 26 2021 at 15:02)</a>:</h4>
<p>I think you're supposed to assume that the old span of memory is exactly the bytes of before, and the new span is all uninit.</p>
<p>But I'm also not sure where that's literally written down.</p>



<a name="227962106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227962106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227962106">(Feb 26 2021 at 17:34)</a>:</h4>
<p>It couldn't be used for stuff like <code>Vec</code> if it didn't preserve contents</p>



<a name="227962247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227962247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227962247">(Feb 26 2021 at 17:35)</a>:</h4>
<p>Preserving up to the old size when growing, or just the new size when shrinking</p>



<a name="227962461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227962461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227962461">(Feb 26 2021 at 17:37)</a>:</h4>
<p>Docs for C's <code>realloc</code> are usually more explicit about this</p>



<a name="227994582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/227994582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Vytautas Astrauskas [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#227994582">(Feb 26 2021 at 21:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="138448">cuviper</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees/near/227962106">said</a>:</p>
<blockquote>
<p>It couldn't be used for stuff like <code>Vec</code> if it didn't preserve contents</p>
</blockquote>
<p>That is true. I guess the documentation should be clarified to be explicit about this.</p>



<a name="228056819"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc%3A%3Arealloc%20guarantees/near/228056819" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/GlobalAlloc.3A.3Arealloc.20guarantees.html#228056819">(Feb 27 2021 at 12:56)</a>:</h4>
<p>Yeah I guess preservation of memory block contents is meant to be implied by saying "shrink or grow"... but yeah this could probably be clarified. Maybe raise this in <a class="stream" data-stream-id="197181" href="/#narrow/stream/197181-t-libs.2Fwg-allocators">#t-libs/wg-allocators</a> ?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>